package com.miguo.activity.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.miguo.activity.LoginActivity;
import com.miguo.activity.base.BaseFragment;
import com.miguo.app.AppHelper;
import com.miguo.view.listener.SingleClickEvent;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.CustomAutoCompleteTextView;
import com.miguo.http.Url;
import com.miguo.ui.R;

/**
 * File Names: LoginFragment
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-08-12
 * Time: 15:22
 */
public class LoginFragment extends BaseFragment implements View.OnClickListener {

    private View contentView;

    private String mUserName;
    private String mPassword;

    private LoginActivity mLoginActivity;

    private CustomAutoCompleteTextView mNameEdt;
    private CustomAutoCompleteTextView mPwdEdt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_login, container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLoginActivity = (LoginActivity) this.getActivity();
        initTitle();
        initView();
    }

    public void initTitle() {
        mLoginActivity.mBaseHead.getRightTextView().setVisibility(View.VISIBLE);
        mLoginActivity.mBaseHead.setTitle(R.string.login_title);
        mLoginActivity.mBaseHead.getRightTextView().setText("注册");
        mLoginActivity.mBaseHead.getRightTextView().setOnClickListener(new SingleClickEvent() {
            @Override
            public void singleClick(View view) {
                mLoginActivity.replaceFragment(RegisterFragment.class);
            }
        });
    }

    private void initView() {
        mNameEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.login_name_edt);
        mPwdEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.login_pwd_edt);
        contentView.findViewById(R.id.login_btn).setOnClickListener(this);

        contentView.findViewById(R.id.login_forget_pwd).setOnClickListener(this);
        contentView.findViewById(R.id.login_open_taobao_tv).setOnClickListener(this);
        contentView.findViewById(R.id.login_open_qq_tv).setOnClickListener(this);

    }

    private void login() {
        mUserName = this.mNameEdt.getText().toString();
        mPassword = this.mPwdEdt.getText().toString();
        if (mUserName.length() == 0) {
            mNameEdt.setShakeAnimation();
            Toast.show(mContext, "请输入用户名");
            return;
        }
        if (mPassword.length() == 0) {
            mPwdEdt.setShakeAnimation();
            Toast.show(mContext, "请输入密码");
            return;
        }

        if (mUserName.length() < 2) {
            mNameEdt.setShakeAnimation();
            Toast.show(mContext, "用户名格式错误");
            return;
        }

        if (mPassword.length() < 6 || mPassword.length() > 16) {
            mPwdEdt.setShakeAnimation();
            Toast.show(mContext, "密码格式错误");
            return;
        }
        mLoginActivity.mUserName = mUserName;
        mLoginActivity.mPassword = mPassword;
        mLoginActivity.login();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_forget_pwd:
                mLoginActivity.replaceFragment(ForgetFragment.class);
                break;
            case R.id.login_btn:
                login();
                break;
            case R.id.login_open_qq_tv:
                AppHelper.StartAuthWeb(mActivity, Url.Passport.Oauth.QQ);
                break;
            case R.id.login_open_taobao_tv:
                AppHelper.StartAuthWeb(mActivity, Url.Passport.Oauth.TaoBao);
                break;
        }
    }
}
